package com.william.jdbcplus.boot.test.controller;

import com.william.jdbcplus.boot.starter.util.DaoUtil;
import com.william.jdbcplus.boot.test.dto.R;
import com.william.jdbcplus.boot.test.dto.ResultCode;
import com.william.jdbcplus.boot.test.entity.SysUser;
import com.william.jdbcplus.common.page.IPage;
import com.william.jdbcplus.common.page.Page;
import com.william.jdbcplus.core.query.EntityWrap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Api(value = "用户测试")
@RestController
@AllArgsConstructor
@Validated
@RequestMapping("/user")
@Slf4j
public class UserController {
    @ApiOperation(value = "业务创建", notes = "业务创建", produces = "application/json")
    @PostMapping(value = "/add")
    public R<String> create(@RequestBody @Valid SysUser user) {
        DaoUtil.BASE.add(user);
        return R.success("成功");
    }

    @ApiOperation(value = "新增多条数据", notes = "新增多条数据", produces = "application/json")
    @PostMapping(value = "/addList")
    public R<String> addList(@RequestBody @Valid List<SysUser> list) {
        DaoUtil.BASE.addList(list);
        return R.success("新增多条成功");
    }

    @ApiOperation(value = "根据id修改", notes = "根据id修改", produces = "application/json")
    @PostMapping(value = "/editById")
    public R<String> editById(@RequestBody @Valid SysUser user) {
        try {
            DaoUtil.BASE.update(user);
            return R.success("成功");
        } catch (Exception e) {
            log.error("根据id修改业务模块异常", e);
            return R.fail(ResultCode.FAILURE);
        }
    }

    @ApiOperation(value = "通过id删除", notes = "根据id删除一条或者多条数据")
    @DeleteMapping(value = "/deleteById")
    public R<String> deleteById(@RequestParam(name = "id", required = true) String id) {
        try {
            DaoUtil.BASE.delete(id, SysUser.class);
            return R.success("删除成功");
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("删除失败，原因：" + e.toString());
        }
    }

    @ApiOperation(value = "根据ids批量删除", notes = "根据ids批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        try {
            String[] idSplit = ids.split(",");
            List<String> pkList = Arrays.asList(idSplit);
            // 删除多条数据
            DaoUtil.BASE.delete(pkList, SysUser.class);
            return R.success("批量删除成功");
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("删除失败，原因：" + e.toString());
        }
    }

    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/page")
    public R<IPage<SysUser>> queryPage() {
        EntityWrap<SysUser> wrap = new EntityWrap<>(SysUser.class);
        wrap.lambda().like(SysUser::getUsername, "小明");
        IPage<SysUser> page = new Page<>(1, 10);
        IPage<SysUser> pageList = DaoUtil.BASE.page(page, wrap);
        return R.data(pageList);
    }

    @ApiOperation(value = "查询列表", notes = "根据条件查询列表数据")
    @PostMapping(value = "/queryList")
    public R<List<SysUser>> queryList() {
        EntityWrap<SysUser> wrap = new EntityWrap<>(SysUser.class);
        wrap.lambda().like(SysUser::getUsername, "小明");
        List<SysUser> list = DaoUtil.BASE.find(wrap);
        return R.data(list);
    }
}
